class InputFormControl extends InputForm implements ChangeEventExtended, ActionEventExtended{

	private var view:InputFormView;
	private var sc:ScrollControl;
	private var label:TextControl;
	private var mcActions:MovieClip;
	private var base:MovieClip;
	private var SCROLL_WIDTH:Number=14;
	private var POS_Y:Number=30;
	private var MARGIN:Number=10;
		
	function InputFormControl(container:MovieClip, name:String, view:InputFormView){
	super(container, name);
		if(view==undefined){
		this.view=new InputFormViewClassic();
		} else {
		this.view=view;
		}
	
	}

	public function getView():InputFormView{
	return view;
	}
	
	public function setFormLabel(label:String){
	this.label.setValue(label);
	super.setFormLabel(label);
	}
	public function setLocation(location:Point):Void {
		super.setLocation(location);
		view.getMask()._x=location.getX();
		view.getMask()._y=location.getY();
		view.getBorder()._x=location.getX();
		view.getBorder()._y=location.getY();
	}

	public function draw(){
	reDraw();
	}
	
	public function reDraw(){
		//Creo el movieBase
		if(base==undefined){
			base=createMovieClip();
			
			base._x=getLocation().getX();
			base._y=getLocation().getY();

			//tengo que setearle propiedades porque el flash es puto
			base.width=getSize().getX()-SCROLL_WIDTH;
			base.height=getSize().getY();

			//le pido a la vista el borde
			view.border(base);
			mcActions=createMovieClip(base);
			mcActions.width=getSize().getX()-SCROLL_WIDTH;
			view.actionsView(actions, mcActions, this);

			//hago el titulo
			this.label=view.label(base);
			this.label.setValue(getFormLabel());
		}
		
		base._x=getLocation().getX();
		base._y=getLocation().getY();
				
		//Dibujo el Scroll (antes lo borro);
		
		
					
		if(checkScroll()){
			super.removeMovieClip(sc.getClip());
			sc=new ScrollControl(base._parent);
			sc.setSize(new Point(SCROLL_WIDTH,base.height+view.getBorderSize()*2));
			sc.setLocation(new Point(base._x+base.width,base._y-view.getBorderSize()));
			sc.setMin(getLocation().getY());
			sc.setMax(getLocation().getY() - ( Math.abs( (view.getMask()._y + view.getMask()._height) - (base._y + base._height)))); 
			sc.setStep(view.getButtonDelta());
			sc.draw();
			sc.addChangeEvent(this);
			view.getBack()._height=base._height+view.getButtonDelta();
			sc.enable();
		} else {
			super.removeMovieClip(sc.getClip());
		}

	}
	
	private function checkScroll():Boolean{
		var f=fields.getItems()[fields.getItems().length-1].getValue();
		var n=f.getLocation().getY()+f.getSize().getY();
		return view.getMask()._height < n + view.getButtonHeight() + view.getButtonDelta();
	}

	public function distributeFields(){
			var nPos=POS_Y;
			for(var i:Number=0; i< fields.getItems().length;i++){
			fields.getItems()[i].getValue().setLocation(new Point(MARGIN+fields.getItems()[i].getValue().getWidthLabel(),nPos));
			nPos+= fields.getItems()[i].getValue().getSize().getY() + view.getSeparator();
			}
			if(!checkScroll()){
			mcActions._y=base._height - view.getButtonDelta()*2 - mcActions._height;
			} else {
			mcActions._y=nPos;
			}
			mcActions._x=(view.getMask()._width - mcActions._width)/2;
			
			draw();
	}

	public function changed(){
	}
	public function changedExtended(source:Control){
		base._y=ScrollControl(source).getValue();
	};

	public function action(){
	}
	public function actionExtended(key:String){
	propagateActionEvent(key);
	};

} //fin clase